package com.yyw.indecatorviewpager;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    ViewPager vp_main;
    LinearLayout ll_dots;
    ImageView iv_move;

    int [] colors={Color.RED,Color.BLUE,Color.YELLOW,Color.GREEN};
    private String TAG="yeyewu";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ll_dots = (LinearLayout) findViewById(R.id.ll_dots);

        vp_main = (ViewPager) findViewById(R.id.vp_main);

        iv_move = (ImageView) findViewById(R.id.iv_move);
        vp_main.setAdapter(new ViewAdapter(this));
        adddots();
        vp_main.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d(TAG,"position="+position+",,positionOffset="+positionOffset+",,positionOffsetPixels="+positionOffsetPixels);

            }

            @Override
            public void onPageSelected(int position) {
                    invalidate(position);
            }




            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void invalidate(int position) {
     for(int i=0;i<ll_dots.getChildCount();i++){
         if(i==position)
             ll_dots.getChildAt(i).setBackgroundResource(R.drawable.shap_dot_select);
         else
             ll_dots.getChildAt(i).setBackgroundResource(R.drawable.shap_dot_unselect);
     }

    }
    private void adddots() {

        for(int i=0;i<colors.length;i++){
            ImageView iv=new ImageView(this);
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.leftMargin=DensityUtils.dp2pex(20,this);
            iv.setLayoutParams(params);

            iv.setBackgroundResource(R.drawable.shap_dot_unselect);
            ll_dots.addView(iv);
        }
    }


    class ViewAdapter extends PagerAdapter{
       Context context;
        ViewAdapter(Context context){
            this.context=context;
        }
        @Override
        public int getCount() {
            return colors.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view=new ImageView(context);
            view.setBackgroundColor(colors[position]);
            container.addView(view);

           return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);

        }
    }
}
